package com.chefu.server.controller;


import com.chefu.common.pojo.PageResult;
import com.chefu.common.pojo.R;

import com.chefu.server.pojo.ServerClassify;
import com.chefu.server.service.ServerClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/serverManager/serverClassify")
public class ServerClassifyController {

    @Autowired
    private ServerClassifyService serveClassifyService;

    /**
     * 新增服务分类
     */
    @PostMapping
    public R saveServeClassify(@RequestParam("code") Long code,@RequestParam("name") String name){
        serveClassifyService.saveServerClassify(code,name);
        return R.ok();
    }

    /**
     * 分页查询服务分类
     * @param pageNum
     * @param pageSize
     * @param code
     * @param name
     * @return
     */
    @GetMapping("/page/{pageNum}/{pageSize}")
    public R serverClassifyPageQuery(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @RequestParam(value = "code",required = false) Long code,
            @RequestParam(value = "name",required = false) String name
    ){
        PageResult<ServerClassify> serveClassifies = serveClassifyService.serverClassifyPageQuery(pageNum,pageSize,code,name);
        return R.ok().data("serveClassifies",serveClassifies);
    }

    /**
     * 修改服务分类
     * @param id
     * @return
     */
    @PutMapping
    public R updateServerClassify(@RequestParam("id") Long id,@RequestParam("name") String name){

        serveClassifyService.updateServerClassify(id,name);
        return R.ok();
    }

    /**
     * 根据服务分类id查询服务分类
     */
    @GetMapping("/server/of/serverClassify")
    public R findServerClassify(@RequestParam("id") Long id){
        ServerClassify serverClassify = serveClassifyService.findServerClassify(id);
        return R.ok().data("serverClassify",serverClassify);
    }

    /**
     * 删除服务分类
     */
    @DeleteMapping
    public R deleteServerClassify(@RequestParam("id") Long id){
        serveClassifyService.deleteServerClassify(id);
        return R.ok().data("error","不能删除");
    }

    /**
     * 根据服务分类父id查询服务子分类
     */
    @GetMapping("/of/sonServerClassify")
    public R sonClassifyByParentId(@RequestParam("parentId") Long parentId){
        List<ServerClassify> serverClassifyList = serveClassifyService.sonClassifyByParentId(parentId);
        return R.ok().data("serverClassifyList",serverClassifyList);
    }

    /**
     * 根据父id添加服务子分类
     */
    @PostMapping("/sonServer")
    public R saveSonClassifyByParentId(
            @RequestParam("parentId") Long parentId,
            @RequestParam("code") Long code,
            @RequestParam("name") String name,
            @RequestParam("generic") Integer generic
    ){
        serveClassifyService.saveSonClassifyByParentId(parentId,code,name,generic);
        return R.ok();
    }

    /**
     * 查询服务子分类管理
     * @param pageNum
     * @param pageSize
     * @param code
     * @param name
     * @return
     */
    @GetMapping("/sonServer/{pageNum}/{pageSize}")
    public R sonClassifyPageQuery(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @RequestParam(value = "code",required = false) Long code,
            @RequestParam(value = "name",required = false) String name
    ){
        PageResult<ServerClassify> serveClassifies = serveClassifyService.sonClassifyPageQuery(pageNum,pageSize,code,name);
        return R.ok().data("serveClassifies",serveClassifies);
    }

    /**
     * 根据子id修改服务子服务
     */
    @PutMapping("/sonServer")
    public R updateSonClassify(@RequestParam("id") Long id,@RequestParam("name") String name){
        serveClassifyService.updateSonClassify(id,name);
        return R.ok();
    }

    /**
     * 根据子id删除服务子分类
     */
    @DeleteMapping("/sonServer")
    public R deleteSonClassifyById(@RequestParam("id") Long id){
        serveClassifyService.deleteSonClassifyById(id);
        return R.ok();
    }

    /**
     * 根据子分类id查找子分类
     */
    @GetMapping("/sonServer")
    public R sonClassifyById(@RequestParam("id") Long id){
        ServerClassify serverClassify = serveClassifyService.sonClassifyById(id);
        return R.ok().data("serverClassify",serverClassify);
    }
}

